package com.mousycoder.mysql.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.annotation.Target;
import java.util.Properties;

/**
 * TODO
 *
 * @author mousycoder
 * @version 1.0
 * @date 2022/3/23 3:55 PM
 */
@Intercepts(
        @Signature(method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class},
                type = Executor.class
        )
)
public class QueryInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        System.out.println("拦截方法开始，SQL执行");
        Object proceed = invocation.proceed();
        System.out.println("拦截方法结束，SQL执行时间" + (System.currentTimeMillis() - startTime) + "ms");
        return proceed;
    }

    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
